﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Odyssey.Linq;
using Odyssey.Web.Models;
using SunTzu.Utility;

namespace Odyssey.Web.Areas.Admin.Controllers
{
    public class UserController : BaseController<tb_user>
    {
        [AcceptVerbs(HttpVerbs.Post)]
        public virtual ActionResult DoLogin()
        {
            string username = System.Web.HttpContext.Current.Request.Form["username"];
            string password = System.Web.HttpContext.Current.Request.Form["password"];
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
                return RedirectToAction("Login", new { Message = "用户名或者密码为空。" });
            tb_user user = table.FirstOrDefault<tb_user>(c => c.username == username);
            if (user == null)
            {
                return RedirectToAction("Login", new { Message = "username error." });
            }
            if (!password.Equals(CryptographyUtility.DecryptString(user.password)))
            {
                return RedirectToAction("Login", new { Message = "password error." });
            }
            SessionManager.User = user;
            SessionManager.UserPermits = db.GetTable<tb_user_permit>().Where(p => p.UserName == user.username).ToList();
            return Redirect("/admin/home");
        }

        public virtual ActionResult Login()
        {
            return View();
        }

        public virtual ActionResult DoLogout()
        {
            SessionManager.User = null;
            SessionManager.UserPermits = null;
            return Redirect("/admin/user/login");
        }

        public virtual ActionResult ModifyPassword()
        {
            return View();
        }

        public virtual ActionResult DoModifyPassword()
        {
            string password = CryptographyUtility.EncryptString(Request.Form["password"]);
            SessionManager.User.password = password;
            var user = db.GetTable<tb_user>().FirstOrDefault(c => c.username == SessionManager.User.username);
            if (user != null)
            {
                user.password = password;
                db.SubmitChanges();
            }
            return View();
        }

        public virtual ActionResult Add()
        {
            List<tb_role> roles = GetTable<tb_role>().ToList();
            ViewData.Add("Roles", roles);
            return View();
        }

        public virtual ActionResult DoAdd()
        {
            string username = System.Web.HttpContext.Current.Request.Form["username"];
            string password = System.Web.HttpContext.Current.Request.Form["password"];

            tb_user user = new tb_user();
            user.username = username;
            user.password = CryptographyUtility.EncryptString(password);
            user.SaveOrUpdate();
            string role = Request.Form["role"];
            if (!(string.IsNullOrEmpty(role) || "001".Equals(role, StringComparison.OrdinalIgnoreCase)))
            {
                tb_user_permit userPermit = new tb_user_permit();
                userPermit.UserName = username;
                userPermit.RoleCode = role;
                userPermit.PermitCode = " ";
                userPermit.ProjectCode = " ";
                userPermit.SaveOrUpdate();
            }
            return Redirect("/admin/user/list");
        }
    }
}

